package cn.bone.ou.community.controller;

import cn.bone.base.bean.CommonResult;
import cn.bone.base.bean.Ref;
import cn.bone.base.utils.StringUtil;
import cn.bone.ou.community.mapper.HouseMapper;
import cn.bone.ou.community.param.HouseParam;
import cn.bone.ou.community.service.IHouseService;
import com.alibaba.fastjson.JSON;
import org.apache.poi.ss.usermodel.*;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.entity.result.ExcelImportResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ou/house")
public class HouseController {

    @Resource
    private IHouseService houseService;

    @RequestMapping("/list")
    public CommonResult list(@Valid HouseParam.ListParam param){
        return houseService.list(param);
    }

    @RequestMapping("/add")
    public CommonResult add(@Valid HouseParam.AddParam param){
        return houseService.add(param);
    }

    @RequestMapping("/modify")
    public CommonResult modify(@Valid HouseParam.ModifyParam param){
        return houseService.modify(param);
    }

    @RequestMapping("/usable")
    public CommonResult usable(@Valid HouseParam.UsableParam param){
        return houseService.usable(param);
    }

    @RequestMapping("/disable")
    public CommonResult disable(@Valid HouseParam.DisableParam param){
        return houseService.disable(param);
    }

    @RequestMapping("/delete")
    public CommonResult delete(@Valid HouseParam.DeleteParam param){
        return houseService.delete(param);
    }

    @RequestMapping("/excelImport")
    public CommonResult excelImport(@RequestParam("file")MultipartFile file,Integer communityId){
        if(file==null || file.isEmpty()){
            return Ref.error("没有文件上传");
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        params.setNeedVerfiy(true);
        ExcelImportResult<HouseParam.ExcelImportParam> result = null;
        try (InputStream inputStream = file.getInputStream()){
            result = ExcelImportUtil.importExcelVerify(inputStream,HouseParam.ExcelImportParam.class,params);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(result==null || result.isVerfiyFail()||result.getList()==null || result.getList().size()==0){
            return Ref.error("excel格式不正确");
        }
        return houseService.excelImport(result.getList(),communityId);
    }

    @RequestMapping("/excelImportTemplate")
    public void excelImportTemplate(HttpServletRequest request, HttpServletResponse response){
        List<HouseParam.ExcelImportParam> list = new ArrayList<>();

        HouseParam.ExcelImportParam p1 = new HouseParam.ExcelImportParam();
        p1.setBuildNum("1");
        p1.setUnitNum("2");
        p1.setHouseNum("1201");

        HouseParam.ExcelImportParam p2 = new HouseParam.ExcelImportParam();
        p2.setBuildNum("3");
        p2.setUnitNum("1");
        p2.setHouseNum("0502");
        p2.setSaleArea(123.23);
        p2.setRealArea(115.88);
        list.add(p1);
        list.add(p2);
        ExportParams exportParams = new ExportParams("房屋信息","房屋", ExcelType.XSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,HouseParam.ExcelImportParam.class,list);
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(1);
        Cell cell = row.createCell(6);
        cell.setCellValue("备注：为保证数据能正常导入，建议将数据单元格的格式设置成文本。选中数据单元格->右键->设置单元格格式->数字->文本");

        Font font = workbook.createFont();
        font.setColor(Font.COLOR_RED);
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);
        String fileName = "导入房屋信息.xlsx";
        String userAgent = request.getHeader("User-Agent");
        boolean isMSIE = StringUtil.isMSBrowser(userAgent);
        try {
            if(isMSIE){
                fileName = URLEncoder.encode(fileName, "UTF-8");
            }else{
                fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=\"" + fileName + "\"");
        try {
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Resource
    private HouseMapper houseMapper;
    @RequestMapping("/test")
    public CommonResult test(String houseNum){
        int i = houseMapper.ignoreInsert(1, "3", "2", houseNum,
                null,null,1,
                0, 1, 3, new Date());
        return Ref.ok(i);
    }
}
